package com.jsnider.pokedex;

import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class AddPokemon extends Activity {
 
    // Progress Dialog
    private ProgressDialog pDialog;
 
    JSONParser jsonParser = new JSONParser();
    EditText p_name;
    EditText p_type;
    EditText p_number;
    EditText p_info;
    Button btnCreate;
 
    // url to create new product
    private static String url_create_product = "http://compsci.lewiscenter.org/~jsnider/1styear/pokemon_create.php";
 
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_POKEMON = "pokemon";
    private static final String TAG_P_ID = "p_id";
    private static final String TAG_P_NAME = "p_name";
    private static final String TAG_P_TYPE = "p_type";
    private static final String TAG_P_NUMBER = "p_number";
    private static final String TAG_P_INFO = "p_info";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_pokemon);
 
        // Edit Text
        p_name = (EditText) findViewById(R.id.p_name);
        p_number = (EditText) findViewById(R.id.p_number);
        p_info = (EditText) findViewById(R.id.p_info);
        p_type = (EditText) findViewById(R.id.p_type);
 
        // Create button
        btnCreate = (Button) findViewById(R.id.btnCreate);
 
        // button click event
        btnCreate.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View view) {
                // creating new pokemon in background thread
                new CreateNewPokemon().execute();
            }
        });
    }
 
    /**
     * Background Async Task to Create new product
     * */
    class CreateNewPokemon extends AsyncTask<String, String, String> {
 
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AddPokemon.this);
            pDialog.setMessage("Creating Pokemon..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
 
        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String name = p_name.getText().toString();
            String type = p_type.getText().toString();
            String info = p_info.getText().toString();
            String number = p_number.getText().toString();
 
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair(TAG_P_NAME, name));
            params.add(new BasicNameValuePair(TAG_P_TYPE, type));
            params.add(new BasicNameValuePair(TAG_P_NUMBER, number));
            params.add(new BasicNameValuePair(TAG_P_INFO, info));
 
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);
 
            // check log cat fro response
            Log.d("Create Response", json.toString());
 
            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
 
                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), AllPokemonActivity.class);
                    startActivity(i);
 
                    pDialog.dismiss();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
 
            return null;
        }
 
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            finish();
        }
 
    }
}